﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       ReadImage
- 机器名称：       YTE
- 命名空间：       LD.Tool.Vision
- 文 件 名：       ReadImage
- 创建时间：       2022/7/14 15:11:16
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using HalconDotNet;
using LD.Host;
using LD.Vision;
using System;

namespace LD.Tool
{
    [Tool(FrmParam = "ToolFindShapeFrm", Text = "查找模板", ToolType = ToolEnum.Vision)]

    class FindShape : BaseTool
    {
        public override OpResult Execute(TB_Tool model)
        {
            stopwatch.Start();
            try
            {
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<CreateShapeParam>(model.Param);
                if (param != null)
                {
                    if (CameraFactory.Instance.CameraList.ContainsKey(param.Camera))
                    {
                        var camera = CameraFactory.Instance.CameraList[param.Camera];
                        var image = FrmVisionMain.Instance.HWindow[camera.Name].Image;
                        if (image != null)
                        {
                            var shapeModel = camera.ShapeModels[param.Name];
                            image.FindShapeModel(shapeModel,
                                                 param.FindModelParam.StartAngle,
                                                 param.FindModelParam.EndAngle,
                                                 param.FindModelParam.MinScore,
                                                 param.FindModelParam.NumMatches,
                                                 param.FindModelParam.MaxOverlap,
                                                 param.FindModelParam.SubPixel,
                                                 param.FindModelParam.NumLeavel,
                                                 param.FindModelParam.Greediness,
                                                 out HTuple row,
                                                 out HTuple column,
                                                 out HTuple angle,
                                                 out HTuple score);
                            if (row > 0)
                            {
                                HHomMat2D mat2D = new HHomMat2D();
                                mat2D.HomMat2dIdentity();
                                var tran = mat2D.HomMat2dTranslate(row, column);
                                var rotate = tran.HomMat2dRotate(angle, row, column);

                                var xld = shapeModel.GetShapeModelContours(1);
                                var xldCont = rotate.AffineTransContourXld(xld);
                                HOperatorSet.SetLineWidth(FrmVisionMain.Instance.HWindow[camera.Name].hWindowControl.HalconWindow, 2.5);
                                FrmVisionMain.Instance.HWindow[camera.Name].DispObj(xldCont, "green");
                                FrmVisionMain.Instance.HWindow[camera.Name].DispText("成功", "green");

                                isSuccess = true;
                                Log(model, $"匹配成功,分数:{score}", Host.LogLevel.一般);

                                xldCont.Dispose();
                                xld.Dispose();
                                rotate = null;
                                tran = null;
                                mat2D = null;
                            }
                            else
                                FrmVisionMain.Instance.HWindow[camera.Name].DispText("失败", "red");

                        }
                    }
                }
                Log(model, "运行结束", Host.LogLevel.严重);
            }
            catch (Exception ex)
            {
                Log(model, ex.Message, Host.LogLevel.严重);
            }
            finally
            {
                stopwatch.Stop();
            }
            return new OpResult() { IsSuccess = isSuccess, ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
    }
}
